package com.qidian.mes.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qidian.mes.mapper.RouteMapper;
import com.qidian.mes.pojo.Route;
import com.qidian.mes.service.RouteService;
import com.qidian.mes.util.pojo.MESResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class RouteServiceImpl implements RouteService {
    @Autowired
    private RouteMapper routeMapper;
    @Override
    public MESResult list(int page, int limit, Route route) {
        PageHelper.startPage(page,limit);
        List<Route> list= routeMapper.list(route);
        PageInfo<Route> pageInfo = new PageInfo<>(list);
        return MESResult.ok(pageInfo);
    }

    @Override
    public MESResult save(Route route) {
        if (routeMapper.getByRouteCode(route.getRouteCode()) !=null||
                routeMapper.getByRouteName(route.getRouteName()) !=null){
            return MESResult.duplicate("编码或名称重复");
        }
        routeMapper.save(route);
        return MESResult.ok();
    }

    @Override
    public MESResult delete(Long routeId) {
        routeMapper.deleteByRouteId(routeId);
        return MESResult.ok();
    }

    @Override
    public MESResult selectById(Long routeId) {
        Route route=null;
        if ((route=routeMapper.selectByRouteId(routeId)) !=null){
            return MESResult.ok(route);
        }
        return MESResult.empty();
    }

    @Override
    public MESResult update(Route route) {
        Route route1 = routeMapper.getByRouteCode(route.getRouteCode());
        if (route1 !=null && !route1.getRouteCode().equals(route.getRouteCode())){
            return MESResult.duplicate("编码重复");
        }
        Route route2 = routeMapper.getByRouteName(route.getRouteName());
        if (route2 != null && !route2.getRouteName().equals(route.getRouteName())){
            return MESResult.duplicate("名称重复");
        }
        routeMapper.update(route);
        return MESResult.ok();
    }
}
